package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.FeedbackCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.Feedback;
import com.vanvalt.payhui.modules.pay.service.FeedbackService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;


/**
 * 意见反馈处理
 *
 */
public class FeedbackProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(FeedbackProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private FeedbackService feedbackService;
	
	/**
	 * 构造方法.
	 */	
	public FeedbackProcess() {
		super();
	}

	/**
	 * @param feedbackService
	 */
	public FeedbackProcess(FeedbackService feedbackService){
		super();
		this.feedbackService = feedbackService;
	}
	
	/**
	 * @param userService
	 * @param feedbackService
	 */
	public FeedbackProcess(UserService userService, FeedbackService feedbackService){
		super();
		this.userService = userService;
		this.feedbackService = feedbackService;
	}

	/**
	 * 意见反馈
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand feedback(RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		FeedbackCommand feedbackCommand = new FeedbackCommand(requestCommand.getObject().toString());
		Map<String, Object> map = feedbackCommand.getMap();
		String uid = (String) map.get("uid");
        String title = (String) map.get("title");
        String comment = (String) map.get("comment");
		
		Map<String,Object> resultMap = Maps.newHashMap();
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(title) && StringUtils.isBlank(comment)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
                Feedback feedback = new Feedback();
                feedback.setUserId(uid);
                feedback.setTitle(title);
                feedback.setComment(comment);
                feedback.setCreateBy(user);
                
                feedbackService.save(feedback);
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeMapJSON(resultMap), responseCommand);
        	}
        	
        }
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}

	
}
